package cn.bugstack.types.design.framework.link.model2.chain;

import cn.bugstack.types.design.framework.link.model2.handler.ILogicHandler;

/**
 * @description 业务业务业务    链路，继承封装链，实现逻辑处理器，
 * 直接定义这个类的对象，填充一个责任链，暴露调用链的接口
 */
public class BusinessLinkedList<T, D, R> extends LinkedList<ILogicHandler<T, D, R>> implements ILogicHandler<T, D, R>{

    public BusinessLinkedList(String name) {
        super(name);
    }

    @Override
    public R apply(T requestParameter, D dynamicContext) throws Exception {
        // 获取头节点
        Node<ILogicHandler<T, D, R>> current = this.first;
        do {
            // 应用当前节点的逻辑，向下转移
            ILogicHandler<T, D, R> item = current.item;
            R apply = item.apply(requestParameter, dynamicContext);
            if (null != apply) return apply;

            current = current.next;
        } while (null != current);

        return null;
    }

}
